<?php
Yii::import('application.components.Reference');
Yii::import('application.components.U');
class ReturSalestransController extends GxController
{
    public function actionCreate()
    {
        if (!Yii::app()->request->isAjaxRequest)
            $this->redirect(url('/'));
        if (isset($_POST) && !empty($_POST)) {
            $msg = "Stored data failed.";
            $detils = CJSON::decode($_POST['detil']);
            app()->db->autoCommit = false;
            $transaction = Yii::app()->db->beginTransaction();
            try {
                $model = new Salestrans;
                $ref = new Reference();
                $docref = $ref->get_next_reference(RETURJUAL);
                foreach ($_POST as $k => $v) {
                    if ($k == 'detil') continue;
                    if (is_angka($v)) $v = ($k == 'salestrans_id' || $k == 'customer_id' ||
                        $k == 'bank_id') ? get_number($v) : -get_number($v);
                    $_POST['Salestrans'][$k] = $v;
                }
                $_POST['Salestrans']['doc_ref'] = $docref;
                $_POST['Salestrans']['tgl'] = new CDbExpression('NOW()');
                $model->attributes = $_POST['Salestrans'];
                if (!$model->save())
                    throw new Exception(t('save.model.fail','app',array('{model}'=>'Return Sales')) . CHtml::errorSummary($model));
                foreach ($detils as $detil) {
                    $salestrans_detail = new SalestransDetails;
                    $_POST['SalestransDetails']['barang_id'] = $detil['barang_id'];
                    $_POST['SalestransDetails']['qty'] = -get_number($detil['qty']);
                    $_POST['SalestransDetails']['price'] = get_number($detil['price']);
                    $_POST['SalestransDetails']['disc'] = -get_number($detil['disc']);
                    $_POST['SalestransDetails']['discrp'] = -get_number($detil['discrp']);
                    $_POST['SalestransDetails']['ketpot'] = $detil['ketpot'];
                    $_POST['SalestransDetails']['beauty_id'] = null;
                    $_POST['SalestransDetails']['vat'] = -get_number($detil['vat']);
                    $_POST['SalestransDetails']['vatrp'] = -get_number($detil['vatrp']);
                    $_POST['SalestransDetails']['total_pot'] = -get_number($detil['totalpot']);
                    $_POST['SalestransDetails']['total'] = -get_number($detil['total']);
                    $_POST['SalestransDetails']['bruto'] = -get_number($detil['bruto']);
                    $_POST['SalestransDetails']['salestrans_id'] = $model->salestrans_id;
                    $salestrans_detail->attributes = $_POST['SalestransDetails'];
                    if (!$salestrans_detail->save())
                        throw new Exception(t('save.model.fail','app',array('{model}'=>'Return Sales Detail')) . CHtml::errorSummary($salestrans_detail));
                    $salestrans_detail->save_beauty_tips();
                    if ($salestrans_detail->barang->grup->kategori_id > 1) {
                        U::add_stock_moves(RETURJUAL, $model->salestrans_id, $model->tgl,
                            $salestrans_detail->barang_id, -$salestrans_detail->qty, $model->doc_ref,
                            $salestrans_detail->price);
                    }
                }
                U::add_bank_trans(RETURJUAL, $model->salestrans_id, $model->bank_id, $model->doc_ref, $model->tgl,
                    $model->total, $model->user_id);
                $ref->save(RETURJUAL, $model->salestrans_id, $docref);
                $transaction->commit();
                $msg = t('save.success','app');
                $status = true;
            } catch (Exception $ex) {
                $transaction->rollback();
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'id' => $docref,
                'msg' => $msg));
            Yii::app()->end();
        }
    }
    public function actionIndex()
    {
        $criteria = new CDbCriteria();
        $criteria->addCondition('total < 0 AND DATE(tgl) = :tgl');
        $criteria->params = array(':tgl' => $_POST['tgl']);
        $model = Salestrans::model()->findAll($criteria);
        $total = Salestrans::model()->count($criteria);
        $this->renderJson($model, $total);
    }
    public function actionDetail()
    {
        $criteria = new CDbCriteria();
        $criteria->addCondition('salestrans_id = :salestrans_id');
        $criteria->params = array(":salestrans_id" => $_POST['salestrans_id']);
        $model = SalestransDetails::model()->findAll($criteria);
        $total = SalestransDetails::model()->count($criteria);
        $this->renderJson($model, $total);
    }
}